import {search} from 'nuijs';
export default module.extend(search,{
    _select:function(e){
        var self = this, opts = self._options, data = self.queryData[self._activeIndex], elem = this._activeItem, value = '';

        if(data === undefined || !elem){
            return
        }
        
        self._selectData = data;

        if(self._callback('SelectBefore', [data, e, elem]) === false){
            return
        }

        if(typeof opts.setValue === 'function'){
            value = opts.setValue.call(opts, self, data)
        }
        else if(opts.field){
            value = data[opts.field]
        }

        self.value(value);
        if(!opts.noHide || (!opts.tag.multiple)){
             self.hide();
        }
        self._callback('Select', [data, e, elem]);
    },
    _exec:function(){
        var self = this, opts = self._options;
        if(self._getTarget() && (self.container = self._jquery(opts.container))){
            self._initData();
            self._bindEvent();
        }
        self._callback('Init');
    },
    value:function(data, add, init){
        var self = this, target = self.target, opts = self._options, 
            _class = self.constructor, name = _class.__component_name
        var  $tagContainer = self.$tagContainer;
        if($tagContainer && typeof data === 'object'){
            if(data !== null){
                var array = [], html;
                if(Nui.type(data, 'Array')){
                    array = array.concat(data)
                }
                else if(typeof data === 'object'){
                    if(data.text){
                        data.text = Nui.trim(data.text);
                    }
                    array.push(data)
                }
                if(self._multiple === true || init){
                    var deleteMatch = self._tag.deleteMatch;
                    var isMethod = typeof deleteMatch === 'function';
                    var $last;
                    Nui.each(self.tagData, function(tag){
                        var $elem = tag.$elem, del = false;
                        Nui.each(array, function(data, i){
                            if(data && data.text){
                                if(isMethod){
                                    del = deleteMatch.call(opts, self, data, tag)
                                }
                                else{
                                    del = data.text === tag.text
                                }
                                if(del === true || del === null){
                                    delete array[i];
                                    return false
                                }
                            }
                        })
                        if(del === true){
                            $elem.remove()
                        }
                        else{
                            $last = $elem
                        }
                    })
                }
                else{
                    self.$tags.remove()
                }

                if(add !== false && (html = self._data2html(array))){
                    //插入到最后一个标签后面
                    if($last){
                        $last.after(html)
                    }
                    else{
                        $tagContainer.prepend(html)
                    }
                    //如果滚动容器有滚动条，添加标签滚动到底部
                    self.$tagScroll.scrollTop(19920604)
                }
            }
            else if(self.$tags){
                self.$tags.remove()
            }

            if(data === null || self._tag.clear !== false){
                self.value('')
            }

            self._change()
        }
        else if(target){
            var dom = target.get(0), obj;
            if(typeof data !== 'string'){
                data = ''
            }
            else{
                data = Nui.trim(data)
            }
            self.val = data;
            if(dom && dom.nui){
                Nui.each(dom.nui, function(v, k){
                    if(k !== name && typeof v.value === 'function' && v._setStyle && v._createRules){
                        obj = v;
                        return false
                    }
                })
            }
            if(obj){
                obj.value(data)
            }
            else{
                target.val(data)
            }
            if(!$tagContainer){
                self._callback('Change')
            }
        }
    }
})